package chartadvancedbar;

import java.util.Arrays;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

/**
 * An advanced bar chart with a variety of controls.
 */
public class ChartAdvancedBar extends Application {

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        root.getChildren().add(createChart());
    }

    private BarChart<String, Number> createChart() {
        final String[] years = {"2007", "2008", "2009"};
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis, "$", null));
        final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis);
        // setup chart
        bc.setTitle("Advanced Bar Chart");
        xAxis.setLabel("Year");
        xAxis.setCategories(FXCollections.observableArrayList(Arrays.asList(years)));
        yAxis.setLabel("Price");

        XYChart.Series<String, Number>[] series = new XYChart.Series[3];

        // create sample data
        int[] data = {567, 1292, 2180, 956, 1665, 2450, 800, 1000, 2800};

        for (int i = 0; i < series.length; i++) {
            // add starting data
            series[i] = new XYChart.Series<>();
            series[i].setName("Data Series " + (i + 1));
            for (int j = 0; j < 3; j++)
                series[i].getData().add(new XYChart.Data<>(years[j], data[i * 3 + j]));
        }
        bc.getData().addAll(series);
        return bc;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
